<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>嘻嘻嘻哈哈哈博客</title>
    <link rel="stylesheet" type="text/css" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../static/css/blog.css">
    <script src="../static/js/jquery.min.js"></script>
</head>
<body class="user-select">
<header class="header">
    <nav class="navbar navbar-default" id="navbar1"
         style="opacity: 0.9;z-index: 9999;">
        <div class="container" id="nav">
            <div class="navbar-header logo-site">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#header-navbar" aria-expanded="false"><span class="sr-only"></span> <span
                        class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span></button>
                <h1 class="logo hvr-bounce-in">
                    <a href="/" title="嘻嘻嘻哈哈哈博客">嘻嘻嘻哈哈哈博客</a>
                </h1>
            </div>
        </div>
    </nav>
</header>
<section class="container" id="detail">
    <div class="content-wrap">
        <div class="content">
            <header class="article-header">
                <h1 class="article-title"><a href="javascript:;">Springmvc提交日期类型参数</a></h1>
                <div class="article-meta">
                    <span class="item article-meta-time">
                        <time class="time" data-toggle="tooltip" data-placement="bottom"><i class="glyphicon glyphicon-time"></i> <span>Wed Sep 23 11:57:29 CST 2020</span></time>
                    </span>
                </div>
            </header>
            <article class="article-content" style="height: auto !important;"><ol style="color: rgb(63, 63, 63); font-family: &quot;microsoft yahei&quot;; font-size: 15px;"><li><p style="margin-bottom: 1.1em; padding: 0px;">背景介绍&nbsp;<br>在springmvc框架中，前台传入到后台的form会经过springmvc自动封装到pojo类中，后台接受的时候可以在参数内直接接受这个java类。</p></li><li><p style="margin-bottom: 1.1em; padding: 0px;">传参&nbsp;<br>通常情况下，前台的表单的类型诸如int,string等，都会根据pojo中字段的类型自动转换。所以为我们省去了不少麻烦，但很可惜其中不包括日期类型。</p></li><li><p style="margin-bottom: 1.1em; padding: 0px;">原因&nbsp;<br>因为日期的格式多种多样，spring自身不适合对其进行封装。好在spring给出了便捷的方法给我们自己转换数据类型。</p></li><li><p style="margin-bottom: 1.1em; padding: 0px;">具体实现</p></li></ol><p style="margin-bottom: 1.1em; padding: 0px; color: rgb(63, 63, 63); font-family: &quot;microsoft yahei&quot;; font-size: 15px;">在controller层中，加入以下代码段</p><pre class="prettyprint" name="code" style="white-space: nowrap; position: relative; overflow-y: hidden; margin-bottom: 1.1em; font-family: &quot;Source Code Pro&quot;, monospace; padding: 5px 5px 5px 60px; font-size: 14px; line-height: 1.45; background-color: rgba(128, 128, 128, 0.05); border-width: 0px; border-color: rgb(136, 136, 136); border-radius: 0px;"><code class="hljs java has-numbering" style="display: block; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-family: &quot;Source Code Pro&quot;, monospace; white-space: pre; word-wrap: normal;"><span class="hljs-annotation" style="color: rgb(155, 133, 157);">@InitBinder</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">public</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">initBinder</span>(WebDataBinder binder) {
    SimpleDateFormat dateFormat = <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> SimpleDateFormat(<span class="hljs-string" style="color: rgb(0, 136, 0);">"yyyy-MM-dd"</span>);
    dateFormat.setLenient(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">false</span>);
    binder.registerCustomEditor(Date.class, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">new</span> CustomDateEditor(dateFormat, <span class="hljs-keyword" style="color: rgb(0, 0, 136);">true</span>));<span class="hljs-comment" style="color: rgb(136, 0, 0);">//true:允许输入空值，false:不能为空值</span>
}</code><ul class="pre-numbering" style="position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">1</li><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">2</li><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">3</li><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">4</li><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">5</li><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">6</li></ul></pre><p style="margin-bottom: 1.1em; padding: 0px; color: rgb(63, 63, 63); font-family: &quot;microsoft yahei&quot;; font-size: 15px;">可以解决这个问题。但是这个时候Date类型的参数是null的话，还是会报错。采用另外一种方式则更好，为null也不会报错，就是把请求参数封装为一个vo类，在对应的类属性上加上注解，这样</p><pre class="prettyprint" name="code" style="white-space: nowrap; position: relative; overflow-y: hidden; margin-bottom: 1.1em; font-family: &quot;Source Code Pro&quot;, monospace; padding: 5px 5px 5px 60px; font-size: 14px; line-height: 1.45; background-color: rgba(128, 128, 128, 0.05); border-width: 0px; border-color: rgb(136, 136, 136); border-radius: 0px;"><code class="hljs java has-numbering" style="display: block; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; font-family: &quot;Source Code Pro&quot;, monospace; white-space: pre; word-wrap: normal;"><span class="hljs-annotation" style="color: rgb(155, 133, 157);">@DateTimeFormat</span>(iso = ISO.DATE_TIME, pattern = <span class="hljs-string" style="color: rgb(0, 136, 0);">"w:yyyy"</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> Date startTime;
或者
<span class="hljs-annotation" style="color: rgb(155, 133, 157);">@DateTimeFormat</span>(pattern=<span class="hljs-string" style="color: rgb(0, 136, 0);">"yyyy-MM-dd HH:mm:ss"</span>)
<span class="hljs-keyword" style="color: rgb(0, 0, 136);">private</span> Date lastLoginDate;</code><ul class="pre-numbering" style="position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding: 6px 0px 40px; border-right: 1px solid rgb(221, 221, 221); list-style: none; text-align: right;"><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">1</li><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">2</li><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">3</li><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">4</li><li style="padding: 0px 5px; list-style: none; margin-left: 0px;">5</li></ul></pre><p style="margin-bottom: 1.1em; padding: 0px; color: rgb(63, 63, 63); font-family: &quot;microsoft yahei&quot;; font-size: 15px;">另外如果使用验证框架，方法参数这样写(@Valid XxxParam param, BindingResult binding) ，就能直接通过BindingResult得到验证结果了。</p></article>
            <div class="relates">
                <div class="title">
                    <h3>相关推荐</h3>
                </div>
                <ul>
                    <li>
                        <a href="/news/detail_111.html">Springmvc提交日期类型参数</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</section>
<footer class="footer">
    <div class="container">
        <p>本站[<a href="http://xiaochancy.gitee.io/java/">嘻嘻嘻哈哈哈博客</a>]的部分内容来源于网络，若侵犯到您的利益，请联系站长删除！谢谢！Powered By [
        </p>
    </div>
    <div id="gotop"><a class="gotop"></a></div>
</footer>
<script src="../static/js/bootstrap.min.js"></script>
<script src="../static/js/blog.js"></script>
</body>
</html>
